package com.zh.mp.demo.modules.demo.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zh.mp.demo.annotation.Log;
import com.zh.mp.demo.exception.NotFoundException;
import com.zh.mp.demo.modules.demo.entity.SearchUser;
import com.zh.mp.demo.modules.demo.entity.User;
import com.zh.mp.demo.modules.demo.service.IUserService;
import com.zh.mp.demo.modules.demo.service.impl.UserServiceImpl;
import com.zh.mp.demo.publisher.UserPublisher;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jobob
 * @since 2021-09-27
 */
@Slf4j
@RestController
@RequestMapping("/api/user")
public class UserController {

    private final IUserService userService;

    private final UserPublisher userPublisher;

    @Autowired
    public UserController(IUserService userService, UserPublisher userPublisher){
        this.userService = userService;
        this.userPublisher = userPublisher;
    }

    @GetMapping("/{id}")
    public User getUser(@PathVariable Long id){
        User user = userService.getById(id);
        if (user == null) {
            throw new NotFoundException(1L, "找不到用户信息");
        }
        userPublisher.publish(user.getId(), user.getName());
        return user;
    }

    @GetMapping("/{page}/{limit}")
    public Page<User> getUserList(@PathVariable int page, @PathVariable int limit, SearchUser searchUser){
        QueryWrapper<User> wrapper = queryWrapper(searchUser);
        Page<User> pages = userService.pageList(page, limit, wrapper);
        return pages;
    }

    @Log(value = "test")
    @GetMapping("/")
    public Page<User> getUserList(SearchUser searchUser){
        QueryWrapper<User> wrapper = queryWrapper(searchUser);
        Page<User> pages = userService.pageList(1, 10, wrapper);
        return pages;
    }

    protected QueryWrapper<User> queryWrapper(SearchUser searchUser){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        Boolean sort = searchUser.getSort().toUpperCase().equals("ASC");
        wrapper.orderBy(true, sort, searchUser.getOrderBy());
        if(searchUser.getIds() != null) {
            wrapper.in("id", searchUser.getIds().split(","));
        }
        if(searchUser.getStartCreateTime() != null) {
            wrapper.ge("create_time", searchUser.getStartCreateTime()); // 区间
        }
        if(searchUser.getEndCreateTime() != null) {
            wrapper.lt("create_time", searchUser.getEndCreateTime()); // 区间
        }

        return wrapper;
    }

    @PostMapping("/")
    public synchronized User save(User user){
        if (user.getName() == null) {
            throw new NotFoundException(403L, "用户为空");
        }
        userService.insert(user);
        return user;
    }

    @DeleteMapping("/{id}")
    public User deleteUser(@PathVariable Long id){
        Boolean status = userService.removeById(id);
        return userService.getById(id);
    }

    @PutMapping("/{id}")
    @Transactional(rollbackFor = NotFoundException.class, propagation = Propagation.REQUIRED)
    public User update(@PathVariable Long id, User user){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        User userInfo = userService.queryOne(wrapper);
        if (userInfo == null) {
            throw new NotFoundException(403L, "未查询到该用户");
        }
        user.setId(userInfo.getId());
        return userService.updateUser(user);
    }
}
